Docker CLI Commands
In the previous tutorial, you read up on Docker and discovered its benefits. You also installed and explored Docker Desktop, and signed up for your own account. Now, it’s time to actually start using Docker! In this tutorial, we will be going through the fundamental Docker commands using the Docker CLI. You will be pulling Docker images, listing Docker images, running containers and more!
Helpful prior knowledge
- There are no pre-requisites
Learning Outcomes
By the end of this tutorial, you will be able to:
- Pull a Docker Image
- List existing Docker images
- Run Docker containers
- List running containers
- Start and stop Docker containers
Tutorial Steps
Total steps: 4
-
Step 1: Pull Your First Docker Image
From the previous tutorial, you've installed Docker Desktop, which also included the Docker CLI client.
IMPORTANT:
Before running any Docker commands, do check that Docker is correctly installed by running
docker --versionin your terminal. Your terminal should output your Docker version indicating that Docker is successfully installed and you are ready to start running Docker CLI commands.As this tutorial requires you to run Docker CLI commands, please complete Tutorial 1 - Introduction to Docker and have Docker Desktop installed before participating in this tutorial.
To start, we will be pulling the official Nginx Docker image from Docker Hub. For context, Docker Hub is a hosted repository service that Docker offers for finding and sharing container images with your team. It enables developers to quickly search and pull certified and community provided Docker images for their development.
Nginx is one of the most widely used open source web servers. A web server simply put is a program that uses HTTP to serve files to users as a response.
Navigate to Nginx’s official image on Docker Hub and you should see the official Nginx build with the command to pull the Nginx Docker image.
Nginx Docker Image on Docker Hub For Windows Users
We recommend that you use WSL (Windows Subsystem for Linux) for this campaign to ensure that you will be able to run the following Docker commands without any issues.
To install WSL, open your Windows terminal/command and enter the following command to install the Ubuntu version of WSL.
wsl --installOnce you’ve installed WSL, open up the terminal.
Proceed to run the command docker pull nginx and Docker will execute the command to pull the Nginx Docker image from Docker Hub. After running the command, your terminal should show that the Nginx Docker image is successfully pulled:
When you run
docker pullwithout any tags, the latest image version will be pulled from Docker Hub by default.In some circumstances, you may prefer to use a fixed version of a picture rather than using the latest image version. Docker enables you to retrieve an image of your choice and ensures that the image you use is always the same by allowing you to specify which version of an image to download.
To pull other versions of the Nginx Docker image, navigate to the “Tags” section under the tags of Nginx to view the different tags available for you to pull.
To pull the Nginx version of 1.22, you can run
docker pull nginx:1.22to get your desired image version.Next, to view all Docker images on your terminal, run
docker images. The default docker images command will show all images, their repository, tags and their size.You can refer to the official docker images documentation on the different options available such as filtering output based on conditions provided.
-
Step 2: Running Nginx Docker Container
In Step 1, you pulled the Nginx Docker image from Docker Hub. Next, it is time to spin up your first Docker container using the image.
Run the command below to start your Nginx Docker container:
IMPORTANT: Remember to define your container name when running the docker run command.
The docker detached mode shown by the option
-dmeans that the docker container created will be running in the background without being attached to your terminal.The
--nameflag option allows you to define your container name to add meaning to your container. If the--nameoption is not assigned, a random string name will be generated making it difficult for you to identify your created container.The
-poption tells Docker to map the port exposed in the container by the Nginx image, which is by default port 80 to the specified port on the host terminal which is port 8080.You can refer to the official docker run documentation to read up on the available options when running your Docker containers.
After running the docker run command, your Nginx container is now successfully running. To view your running Nginx container, run the command
docker psas shown:Further, you can also navigate to your Docker Desktop. It will show your running Nginx container with the container name, image used and status.
Next, navigate to localhost:8080 where you will observe that Nginx is successfully installed and running.
-
Step 3: Start and Stop Docker Container
Now that you have learnt how to run a Docker container from a Docker image, it’s time to learn how to start and stop Docker containers.
To stop the running Nginx Docker container, run the command
docker stop <container-name>IMPORTANT: Remember to use the right container name when running the docker stop command
Then, execute the command
docker ps -awhich displays all containers, whether they are running or not. According to the output, the container should have exited and stopped running.Your Docker Desktop should also show that your Nginx container has exited and is no longer running.
To start the stopped container, run the command
docker start <container-name>IMPORTANT: Remember to use the same container name when running the docker start command
After running the Docker start command, proceed to run the command
docker ps -aand you should see that your Nginx container is up and running again.Apart from interacting with the CLI, Docker Desktop also allows you to start and stop Docker containers using their user interface under the Actions tab to provide a more intuitive experience.
You should now understand why Docker is important for DevOps. This is because it allows users to share Docker images and run their applications in Docker containers. By running applications in Docker containers, developers ensure that their applications are compatible and deployable on any machine, eliminating the "it works on mine, but not on yours" dilemma.
-
Step 4: Testing Your Knowledge
You’ve learnt quite a bit about Docker Containers in this tutorial! Time to put your knowledge to the test
- Go to the link here to get access to the quiz
- Start & Complete the Quiz. A good score to achieve would be 8 out of 10
Find articles to support you through your journey or chat with our support team.
Help Center